<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
    <style>
        .layui-form-select dl dd, .layui-form-select dl dt {
            height: 36px;
        }
    </style>
</head>

<body class='labelAuto'>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">仓库管理</a>
            <a>
                <cite>仓库物资</cite>
            </a>
        </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body search">
                    <form action="" class="layui-form">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">材料类型</label>
                                <div class="layui-input-block">
                                    <select name="type" lay-verify="required" id="materialType">
                                        <option value="">-</option>
                                        <option value="1">消耗</option>
                                        <option value="2">销售</option>
                                        <option value="3">固定资产</option>
                                        <option value="4">租赁</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">搜索</label>
                                <div class="layui-input-block">
                                    <input type="search" autocomplete="off" class="layui-input" id="searchInput">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn search" type="button" id='search'>
                                    <i class="iconfont layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body tableCardBody">
                    <table class="layui-table" id="lists" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type='text/html' id='toolbar'>
    <button class="layui-btn" lay-event="log" type="button" auth="warehouseLog">出入库记录</button>
    <button class="layui-btn" lay-event="rate" type="button" auth="saleRate">加价率</button>
    <button class="layui-btn" lay-event="checkPurchase" type="button" auth="checkPurchase">入库验收</button>
    <button class="layui-btn" lay-event="purchase" type="button" auth="purchase">项目采购</button>
    <button class="layui-btn" lay-event="adjust" type="button">材料调拨</button>
    <button class="layui-btn" lay-event="adjustLog" type="button">调拨明细</button>
    <button class="layui-btn" lay-event="expend" type="button" auth="expend">消耗出库</button>
    <button class="layui-btn" lay-event="consume" type="button" auth="consume">工人领料</button>
    <button class="layui-btn" lay-event="group-consume" type="button" auth="groupConsume">班组领料</button>
</script>
<script src="../statics/plugins/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/javascript">
    layui.use(['form', 'layer', "table", "laydate", 'upload'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var projectId = Number(getParam('projectId'));
        // 异步数据加载
        var lists = table.render({
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '人员列表',
            height: 'full-150',
            contentType: "application/json; charset=utf-8",
            url: url + '/warehouse/warehouse/lists', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            method: 'POST',
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count": res.data.count
                };
            },
            where: {
                projectId: projectId,
                type: '',
                search: '',
                token: getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: ['filter', {title: '导入', layEvent: 'upload', icon: 'layui-icon-upload-circle'}],
            page: true, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [
                [ //表头
                    {field: '', title: '编号', type: 'numbers', width: '6%'},
                    {field: 'materialName', title: '材料名称', width: '12%'},
                    {field: 'spec', title: '规格型号', width: '12%'},
                    {
                        field: 'amount', title: '当前库存数量', width: '10%', templet:function(d) {
                            return Number(d.amount);
                        }
                    },
                    {field: 'unitName', title: '单位', width: '8%'},
                    {field: 'purchasePrice', title: '收储价格（元）', width: '10%'},
                    {
                        field: 'salePrice', title: '仓库售价（元）', width: '10%', templet: function (d) {
                            return d.salePrice == null ? '-' : d.salePrice;
                        }
                    },
                    {
                        field: '', title: '操作', width: '18%', templet: function (d) {
                            return `<button class='layui-btn' data-id=${d.id} auth='salePrice' lay-event='setPrice'>售价</button>
                                    <button class='layui-btn' data-id=${d.id} auth='breakdown' lay-event='breakdown'>报损</button>
                                    <button class='layui-btn' data-id=${d.id} lay-event='log'>使用情况</button>`;
                        }
                    },
                ]
            ],
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            done: function () {
                permission();
            }
        });

        table.on('toolbar(test)', function (data) {
            if (data.event == 'rate') {
                var rate = 0;
                $.ajax({
                    type: "post",
                    url: url + '/setting/setting/getRate',
                    async: false,
                    data: {
                        projectId: Number(getParam('projectId')),
                        token: getParam('token')
                    },
                    success: function (r) {
                        rate = r.data;
                    }
                });
                layer.open({
                    type: 1,
                    title: "加价率",
                    area: ['40%', '30%'],
                    content: `<form class='layui-form layerOpen'>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>加价率(%)</label>
                                <div class='layui-input-block'>
                                    <input type='number' name="lowPrice" class='layui-input' value="${Number(rate) * 100}">
                                </div>
                            </div>
                        </div>
                    </form>`,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    yes: function (index, layero) {
                        var salePrice = Number($('input[name=salePrice]').val());
                        var lowPrice = Number($('input[name=lowPrice]').val());

                        if (salePrice < lowPrice) {
                            tips('售价', {msg: '售价不能小于材料底价'})
                        }
                        $.ajax({
                            url: url + "/setting/setting/setSalePrice",
                            type: "POST",
                            async: false,
                            data: {
                                id: Number(data.data.id),
                                salePrice: salePrice,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('售价', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('售价', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'checkPurchase') {
                window.location.href = 'warehouseSupplierOrderLists.html?projectId=' + projectId
            } else if (data.event == 'purchase') {
                layer.open({
                    type: 1,
                    title: "采购",
                    area: ['70%', '90%'],
                    content: `
                    <form class='layui-form layerOpen' lay-filter="layerForm">
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>采购时间</label>
                                <div class='layui-input-block' style="width:20%;">
                                    <input type="text" id="deliveryTime" class="layui-input" name="deliveryTime" value="">
                                </div>
                            </div>
                        </div>
                        <div class='layui-item supplier'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>供应商</label>
                                <div class='layui-input-block' style="width:20%">
                                    <select name="supplierId" lay-filter="supplierId" lay-search>${supplierSelect(true)}</select>
                                </div>
                                <div class='layui-input-block' style="width:12%;min-width: 150px;display:none">
                                    <input type="text" class="layui-input" name="name" placeholder="请输入供应商名称">
                                </div>
                                <div class='layui-input-block' style="width:10%;min-width: 95px;display:none">
                                    <input type="text" class="layui-input" name="phone" placeholder="请输入联系方式">
                                </div>
                                <div class='layui-input-block' style="width:17%;display:none">
                                    <input type="text" class="layui-input" name="address" placeholder="请输入地址">
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>支付方式</label>
                                <div class='layui-input-block' style="width:20%;">
                                    <select name="payType" lay-search>
                                        <option value="">-</option>
                                        <option value="1">现金</option>
                                        <option value="2">对公支付</option>
                                        <option value="3">后付款</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>单据总额</label>
                                <div class='layui-input-block'>
                                    <input type="number" min="0" class="layui-input" name="tPrice" value="0">
                                </div>
                            </div>
                        </div>
                        <div class='layui-item detail'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>采购明细</label>
                                <div class='layui-input-block' style="width:25%">
                                    <select name="materialId" lay-filter="materialId" lay-search>${materialSelect()}</select>
                                </div>
                                <div class='layui-input-block' style="width:15%">
                                    <select name="specId" lay-filter="specId" lay-search></select>
                                </div>
                                <div class='layui-input-block' style="width:8%">
                                    <input type="number" min="0" step="0.0001" class="layui-input" name="amount" placeholder="输入数量">
                                </div>
                                <div class='layui-input-block' style="width:8%">
                                    <input type="text" class="layui-input" name="unitId" value=""  data-id="" disabled placeholder="计量单位">
                                </div>
                                <div class='layui-input-block' style="width:9%">
                                    <input type="number" min="0" step="0.01" class="layui-input" name="price" placeholder="输入单价">
                                </div>
                                <div class='layui-input-block' style="width:15%">
                                    <input type="text" class="layui-input" name="totalPrice" value="">
                                </div>
                                <div class='layui-input-block' style="width:2%">
                                    <button type="button" class="layui-btn caigou btn btn-plus" style="width:26px">+</button>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>备注</label>
                                <div class='layui-input-block'>
                                    <input type="text" class="layui-input" name="remark" value="">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        laydate.render({
                            elem: '#deliveryTime'
                            , type: 'date'
                            , max: 0
                        });
                        form.render();
                    },
                    yes: function (index, layero) {
                        var supplierId = $('select[name=supplierId]').val();
                        var name = '';
                        var phone = '';
                        var address = '';
                        if (supplierId == 0) {
                            name = $('input[name=name]').val();
                            phone = $('input[name=phone]').val();
                            address = $('input[name=address]').val();
                            if (name == '' || address == '') {
                                tips('采购', {msg: '请完整填写供应商名称及地址'});
                            }
                        } else if (supplierId == '') {
                            tips('采购', {msg: '请选择材料供应商'});
                        }
                        var payType = $('select[name=payType]').val();
                        var tPrice = $('input[name=tPrice]').val();
                        var remark = $('input[name=remark]').val();
                        var deliveryTime = $('input[name=deliveryTime]').val();
                        if (deliveryTime == '') {
                            tips('采购', {msg: '请选择采购时间'});
                        }
                        var inline = $('.detail div.layui-inline');
                        var data = [];
                        for (var i = 0; i < inline.length; i++) {
                            materialId = Number($(inline[i]).find('select[name=materialId] option:selected').val());
                            specId = Number($(inline[i]).find('select[name=specId]').val());
                            amount = Number($(inline[i]).find('input[name=amount]').val());
                            unitId = Number($(inline[i]).find('select[name=specId] option:selected').data('unitid'));
                            price = Number($(inline[i]).find('input[name=price]').val());
                            totalPrice = Number($(inline[i]).find('input[name=totalPrice]').val());
                            if (materialId == '' || amount == '' || price == '' || totalPrice=='') {
                                tips('采购', {msg: '采购材料信息不完整'});
                                return false
                            }

                            data.push({
                                materialId: materialId,
                                specId: specId,
                                amount: amount,
                                unitId: unitId,
                                price: price,
                                totalPrice: totalPrice
                            })
                        }
                        $.ajax({
                            url: url + "/warehouse/warehouse/purchase",
                            type: "POST",
                            data: {
                                projectId: projectId,
                                price: tPrice,
                                supplierId: Number(supplierId),
                                name: name,
                                phone: phone,
                                address: address,
                                payType: payType,
                                remark: remark,
                                deliveryTime: deliveryTime,
                                data: data,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('采购', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('采购', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'receipt') {
                layer.open({
                    type: 1,
                    title: "入库",
                    area: ['65%', '70%'],
                    content: `
                    <form class='layui-form layerOpen' lay-filter="layerForm">
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>入库时间</label>
                                <div class='layui-input-block' style="width:15%;">
                                    <input type='text' name='time' autocomplete="off" class='layui-input'>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item supplier'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>来源</label>
                                <div class='layui-input-block' style="width:15%">
                                    <select name="sourceProjectId" lay-filter="sourceProjectId" lay-search>${projectSelect()}</select>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>单据总额</label>
                                <div class='layui-input-block'>
                                    <input type="number" min="0" class="layui-input" name="tPrice" value="0">
                                </div>
                            </div>
                        </div>
                        <div class='layui-item detail'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>入库明细</label>
                                <div class='layui-input-block' style="width:15%">
                                    <select name="materialId" lay-filter="materialId" lay-search>${materialSelect()}</select>
                                </div>
                                <div class='layui-input-block' style="width:12%">
                                    <select name="specId" lay-search>
                                        <option value="">选择规格</option>
                                    </select>
                                </div>
                                <div class='layui-input-block' style="width:12%">
                                    <input type="number" min="0" class="layui-input" name="amount" placeholder="输入数量">
                                </div>
                                <div class='layui-input-block' style="width:12%">
                                    <input type="text" class="layui-input" name="unitId" value=""  data-id="" disabled placeholder="计量单位">
                                </div>
                                <div class='layui-input-block' style="width:12%">
                                    <input type="number" min="0" class="layui-input" name="price" placeholder="材料单价">
                                </div>
                                <div class='layui-input-block' style="width:15%">
                                    <input type="text" class="layui-input" name="totalPrice" value="" disabled placeholder="材料总价">
                                </div>
                                <div class='layui-input-block' style="width:2%">
                                    <button type="button" class="layui-btn btn btn-plus" style="width:26px">+</button>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>备注</label>
                                <div class='layui-input-block'>
                                    <input type="text" class="layui-input" name="remark" value="">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        laydate.render({
                            elem: 'input[name=time]'
                            , type: 'date'
                            , value: new Date()
                            , max: 0
                        });
                        form.render();
                    },
                    yes: function (index, layero) {
                        var projectId = getParam('projectId');
                        var time = $('input[name=time]').val();
                        var tPrice = $('input[name=tPrice]').val();
                        var remark = $('input[name=remark]').val();
                        var inline = $('.detail div.layui-inline');
                        var data = [];
                        for (var i = 0; i < inline.length; i++) {
                            materialId = Number($(inline[i]).find('select[name=materialId]').val());
                            specId = Number($(inline[i]).find('select[name=specId]').val());
                            amount = Number($(inline[i]).find('input[name=amount]').val());
                            unitId = Number($(inline[i]).find('input[name=unitId]').data('id'));
                            price = Number($(inline[i]).find('input[name=price]').val());

                            if (materialId == '' || specId == '' || amount == '' || unitId == '' || price == '') {
                                tips('入库', {msg: '入库信息不完整'});
                            }

                            data.push({
                                materialId: materialId,
                                specId: specId,
                                amount: amount,
                                unitId: unitId,
                                price: price
                            })
                        }
                        $.ajax({
                            url: url + "/warehouse/warehouse/receipt",
                            type: "POST",
                            data: {
                                projectId: projectId,
                                time: time,
                                price: tPrice,
                                supplierId: supplierId,
                                name: name,
                                phone: phone,
                                address: address,
                                payType: payType,
                                remark: remark,
                                data: data,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('入库', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('入库', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'expend') {
                layer.open({
                    type: 1,
                    title: "出库",
                    area: ['65%', '70%'],
                    content: `
                    <form class='layui-form layerOpen' lay-filter="layerForm">
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>出库时间</label>
                                <div class='layui-input-block' style="width:15%;">
                                    <input type='text' name='time' autocomplete="off" class='layui-input'>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item detail'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>出库明细</label>
                                <div class='layui-input-block' style="width:180px">
                                    <select name="id" lay-filter="id" lay-search>${warehouseSelect()}</select>
                                </div>
                                <div class='layui-input-block' style="width:90px">
                                    <select name="specId" lay-filter="specId" lay-search><<option value="">-</option></select>
                                </div>
                                <div class='layui-input-block' style="width:80px">
                                    <input type="number" min="0" step="0.0001" class="layui-input" name="amount" placeholder="输入数量">
                                </div>
                                <div class='layui-input-block' style="width:70px">
                                    <input type="text" class="layui-input" name="unitId" value="" disabled placeholder="计量单位">
                                </div>
                                <div class='layui-input-block' style="width:70px">
                                    <input type="number" min="0" step="0.01" class="layui-input" name="price" placeholder="材料单价" disabled>
                                </div>
                                <div class='layui-input-block' style="width:90px">
                                    <input type="text" class="layui-input" name="totalPrice" value="" disabled placeholder="材料总价">
                                </div>
                                <div class='layui-input-block' style="width:30px">
                                    <button type="button" class="layui-btn chuku btn btn-plus" style="width:26px">+</button>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>备注</label>
                                <div class='layui-input-block'>
                                    <input type="text" class="layui-input" name="remark" value="">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        laydate.render({
                            elem: 'input[name=time]'
                            , type: 'date'
                            , value: new Date()
                            , max: 0
                        });
                        form.render();
                    },
                    yes: function (index, layero) {
                        var projectId = getParam('projectId');
                        var time = $('input[name=time]').val();
                        var remark = $('input[name=remark]').val();
                        var inline = $('.detail div.layui-inline');
                        var data = [];
                        for (var i = 0; i < inline.length; i++) {
                            id = Number($(inline[i]).find('select[name=specId] option:selected').data('id'));
                            amount = Number($(inline[i]).find('input[name=amount]').val());
                            if (id == '' || amount == '') {
                                tips('出库', {msg: '请填写出库材料及数量'});
                            }
                            data.push({id: id, amount: amount})
                        }
                        $.ajax({
                            url: url + "/warehouse/warehouse/expend",
                            type: "POST",
                            data: {
                                projectId: projectId,
                                time: time,
                                remark: remark,
                                data: data,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('出库', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('出库', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'consume') {
                layer.open({
                    type: 1,
                    title: "出库",
                    area: ['65%', '70%'],
                    content: `
                    <form class='layui-form layerOpen' lay-filter="layerForm">
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>出库时间</label>
                                <div class='layui-input-block' style="width:15%;">
                                    <input type='text' name='time' autocomplete="off" class='layui-input'>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>查询姓名</label>
                                <div class='layui-input-block'>
                                    <input type='text' placeholder='请输入姓名或工号' name='search' autocomplete="off" class='layui-input'>
                                    <ul class='layerOpen_ul_list'></ul>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item detail'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>出库明细</label>
                                <div class='layui-input-block' style="width:180px">
                                    <select name="id" lay-filter="id" lay-search>${warehouseSelect()}</select>
                                </div>
                                <div class='layui-input-block' style="width:90px">
                                    <select name="specId" lay-filter="specId" lay-search><<option value="">-</option></select>
                                </div>
                                <div class='layui-input-block' style="width:80px">
                                    <input type="number" min="0" step="0.0001" class="layui-input" name="amount" placeholder="输入数量">
                                </div>
                                <div class='layui-input-block' style="width:70px">
                                    <input type="text" class="layui-input" name="unitId" value="" disabled placeholder="计量单位">
                                </div>
                                <div class='layui-input-block' style="width:70px">
                                    <input type="number" min="0" step="0.01" class="layui-input" name="price" placeholder="材料单价" disabled>
                                </div>
                                <div class='layui-input-block' style="width:90px">
                                    <input type="text" class="layui-input" name="totalPrice" value="" disabled placeholder="材料总价">
                                </div>
                                <div class='layui-input-block' style="width:30px">
                                    <button type="button" class="layui-btn chuku btn btn-plus" style="width:26px">+</button>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>备注</label>
                                <div class='layui-input-block'>
                                    <input type="text" class="layui-input" name="remark" value="">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        laydate.render({
                            elem: 'input[name=time]'
                            , type: 'date'
                            , value: new Date()
                            , max: 0
                        });
                        form.render();
                    },
                    yes: function (index, layero) {
                        var employeeId = $('input[name=search]').data('id');
                        var projectId = getParam('projectId');
                        var time = $('input[name=time]').val();
                        var remark = $('input[name=remark]').val();
                        var inline = $('.detail div.layui-inline');
                        var data = [];
                        for (var i = 0; i < inline.length; i++) {
                            materialId = Number($(inline[i]).find('select[name=id] option:selected').val());
                            specId = Number($(inline[i]).find('select[name=specId]').val());
                            amount = Number($(inline[i]).find('input[name=amount]').val());
                            if (materialId == '' || amount == '') {
                                tips('出库', {msg: '请填写出库材料及数量'});
                            }
                            data.push({materialId: materialId,specId: specId, amount: amount})
                        }
                        $.ajax({
                            url: url + "/warehouse/warehouse/consume",
                            type: "POST",
                            data: {
                                sourceEmployeeId: employeeId,
                                projectId: projectId,
                                time: time,
                                remark: remark,
                                data: data,
                                isGroup: 0,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('出库', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('出库', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'group-consume') {
                layer.open({
                    type: 1,
                    title: "出库",
                    area: ['65%', '70%'],
                    content: `
                    <form class='layui-form layerOpen' lay-filter="layerForm">
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>出库时间</label>
                                <div class='layui-input-block' style="width:15%;">
                                    <input type='text' name='time' autocomplete="off" class='layui-input'>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>班组</label>
                                <div class='layui-input-block'>
                                    <select name="groupId" id="">${groupSelect(getParam('projectId'))}</select>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item detail'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>出库明细</label>
                                <div class='layui-input-block' style="width:180px">
                                    <select name="id" lay-filter="id" lay-search>${warehouseSelect()}</select>
                                </div>
                                <div class='layui-input-block' style="width:90px">
                                    <select name="specId" lay-filter="specId" lay-search><<option value="">-</option></select>
                                </div>
                                <div class='layui-input-block' style="width:80px">
                                    <input type="number" min="0" step="0.0001" class="layui-input" name="amount" placeholder="输入数量">
                                </div>
                                <div class='layui-input-block' style="width:70px">
                                    <input type="text" class="layui-input" name="unitId" value="" disabled placeholder="计量单位">
                                </div>
                                <div class='layui-input-block' style="width:70px">
                                    <input type="number" min="0" step="0.01" class="layui-input" name="price" placeholder="材料单价" disabled>
                                </div>
                                <div class='layui-input-block' style="width:90px">
                                    <input type="text" class="layui-input" name="totalPrice" value="" disabled placeholder="材料总价">
                                </div>
                                <div class='layui-input-block' style="width:30px">
                                    <button type="button" class="layui-btn chuku btn btn-plus" style="width:26px">+</button>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>备注</label>
                                <div class='layui-input-block'>
                                    <input type="text" class="layui-input" name="remark" value="">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        laydate.render({
                            elem: 'input[name=time]'
                            , type: 'date'
                            , value: new Date()
                            , max: 0
                        });
                        form.render();
                    },
                    yes: function (index, layero) {
                        var groupId = $('select[name=groupId]').val();
                        var projectId = getParam('projectId');
                        var time = $('input[name=time]').val();
                        var remark = $('input[name=remark]').val();
                        var inline = $('.detail div.layui-inline');
                        var data = [];
                        for (var i = 0; i < inline.length; i++) {
                            materialId = Number($(inline[i]).find('select[name=id] option:selected').val());
                            specId = Number($(inline[i]).find('select[name=specId]').val());
                            amount = Number($(inline[i]).find('input[name=amount]').val());
                            if (materialId == '' || amount == '') {
                                tips('出库', {msg: '请填写出库材料及数量'});
                            }
                            data.push({materialId: materialId,specId: specId, amount: amount})
                        }
                        $.ajax({
                            url: url + "/warehouse/warehouse/groupConsume",
                            type: "POST",
                            data: {
                                sourceGroupId: groupId,
                                projectId: projectId,
                                time: time,
                                remark: remark,
                                data: data,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('出库', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('出库', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'upload') {
                layer.open({
                    type: 0,
                    title: "材料导入",
                    area: ["40%", '30%'],
                    content: `点击按钮选择上传文件&nbsp;&nbsp;&nbsp;<button type="button" class="layui-btn layui-btn-xs" id="upload"><i class="layui-icon layui-icon-file"></i>上传文件</button>`,
                    btn: ['关闭'],
                    skin: 'my-skin',
                    success: function () {
                        var uploadInst = upload.render({
                            elem: '#upload' //绑定元素
                            , accept: 'file'
                            , exts: 'xls|xlsx'
                            , data: {token: getParam('token')}
                            , url: url + '/excel/import/warehouseMaterial' //上传接口
                            , done: function (r) {
                                if (r.code == 100000) {
                                    tips('材料导入', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('材料导入', r);
                                }
                                //上传完毕回调
                            }
                            , error: function () {
                                //请求异常回调
                            }
                        });
                    },
                })
            } else if(data.event === 'adjust'){
                let projectOption = '<option value="">-</option>';
                $.ajax({
                    url: url + '/project/project/selectLists',
                    type: "POST",
                    async: false,
                    data: {
                        isAll: 0,
                        token: getToken()
                    },
                    success: function (r) {
                        if (r.code == 100000) {
                            for (var i = 0; i < r.data.length; i++) {
                                projectOption += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                            }
                        } else {
                            tips('项目下拉列表', r);
                        }
                    }
                });
                layer.open({
                    type: 1,
                    title: "调拨",
                    area: ['70%', '90%'],
                    content: `
                    <form class='layui-form layerOpen' lay-filter="layerForm">
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>调拨时间</label>
                                <div class='layui-input-block' style="width:20%;">
                                    <input type="text" id="time" class="layui-input" name="time" value="">
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>接收项目</label>
                                <div class='layui-input-block' style="width:20%">
                                    <select name="projectId" lay-filter="projectId" lay-search>${projectOption}</select>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item detail'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>调拨明细</label>
                                <div class='layui-input-block' style="width:25%">
                                    <select name="id" lay-filter="id" lay-search>${warehouseSelect(projectId)}</select>
                                </div>
                                <div class='layui-input-block' style="width:15%">
                                    <select name="specId" lay-filter="specId" lay-search></select>
                                </div>
                                <div class='layui-input-block' style="width:8%">
                                    <input type="number" min="0" step="0.0001" class="layui-input" name="amount" placeholder="输入数量">
                                </div>
                                <div class='layui-input-block' style="width:8%">
                                    <input type="text" class="layui-input" name="unitId" value=""  data-id="" disabled placeholder="计量单位">
                                </div>
                                <div class='layui-input-block' style="width:2%">
                                    <button type="button" class="layui-btn diaobo btn btn-plus" style="width:26px">+</button>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>备注</label>
                                <div class='layui-input-block'>
                                    <input type="text" class="layui-input" name="remark" value="">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        laydate.render({
                            elem: '#time'
                            , type: 'date'
                            , max: 0
                        });
                        form.render();
                    },
                    yes: function (index, layero) {
                        var newProjectId = $('select[name=projectId]').val();
                        if (newProjectId == '') {
                            tips('调拨', {msg: '请选择调拨至哪个项目仓库'});
                        }
                        var remark = $('input[name=remark]').val();
                        var time = $('input[name=time]').val();
                        if (time == '') {
                            tips('调拨', {msg: '请选择调拨时间'});
                        }
                        var inline = $('.detail div.layui-inline');
                        var data = [];
                        for (var i = 0; i < inline.length; i++) {
                            materialId = Number($(inline[i]).find('select[name=id] option:selected').val());
                            specId = Number($(inline[i]).find('select[name=specId]').val());
                            amount = Number($(inline[i]).find('input[name=amount]').val());
                            unitId = Number($(inline[i]).find('select[name=specId] option:selected').data('unitid'));
                            if (materialId === '' || amount === '' || unitId == 0 ) {
                                tips('调拨', {msg: '调拨材料信息不完整'});
                                return false
                            }

                            data.push({
                                materialId: materialId,
                                specId: specId,
                                amount: amount,
                                unitId: unitId,
                            })
                        }
                        $.ajax({
                            url: url + "/warehouse/warehouse/adjust",
                            type: "POST",
                            data: {
                                projectId: projectId,
                                newProjectId: Number(newProjectId),
                                remark: remark,
                                time: time,
                                data: data,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('调拨', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('调拨', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event === 'adjustLog'){
                window.location.href = 'warehouseAdjustLog.html?projectId=' + projectId;
            }
        });

        table.on('tool(test)', function (data) {
            if (data.event == 'setPrice') {
                var rate = 0;
                $.ajax({
                    type: "post",
                    url: url + '/setting/setting/getRate',
                    async: false,
                    data: {
                        projectId: Number(getParam('projectId')),
                        token: getParam('token')
                    },
                    success: function (r) {
                        rate = r.data;
                    }
                });
                layer.open({
                    type: 1,
                    title: "售价",
                    area: ['40%', '30%'],
                    content: `<form class='layui-form layerOpen'>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>材料底价</label>
                                <div class='layui-input-block'>
                                    <input type='number' name="lowPrice" class='layui-input' value="${Number(data.data.purchasePrice) * (Number(rate) + 1)}" disabled>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>售价</label>
                                <div class='layui-input-block'>
                                    <input type='number'  class='layui-input' min="0" name="salePrice">
                                </div>
                            </div>
                        </div>
                    </form>`,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    yes: function (index, layero) {
                        var salePrice = Number($('input[name=salePrice]').val());
                        var lowPrice = Number($('input[name=lowPrice]').val());

                        if (salePrice < lowPrice) {
                            tips('售价', {msg: '售价不能小于材料底价'})
                        }
                        $.ajax({
                            url: url + "/warehouse/warehouse/setSalePrice",
                            type: "POST",
                            async: false,
                            data: {
                                id: Number(data.data.id),
                                salePrice: salePrice,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('售价', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('售价', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'breakdown') {
                var id = Number(data.data.id);
                layer.open({
                    type: 1,
                    title: "出库",
                    area: ['40%', '30%'],
                    content: `<form class='layui-form layerOpen'>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>出库时间</label>
                                <div class='layui-input-block' style="width:20%;">
                                    <input type='text' name='time' autocomplete="off" class='layui-input'>

                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>报损数量</label>
                                <div class='layui-input-block'>
                                    <input type='number' name="amount" class='layui-input' value="">
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>备注说明</label>
                                <div class='layui-input-block'>
                                    <input type='text'  class='layui-input' name="remark">
                                </div>
                            </div>
                        </div>
                    </form>`,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        laydate.render({
                            elem: 'input[name=time]'
                            , type: 'date'
                            , value: new Date()
                            , max: 0
                        });
                        form.render();
                    },
                    yes: function (index, layero) {
                        var amount = Number($('input[name=amount]').val());
                        var data = [];
                        data.push({id: id, amount: amount});
                        $.ajax({
                            url: url + "/warehouse/warehouse/breakdown",
                            type: "POST",
                            async: false,
                            data: {
                                projectId: getParam('projectId'),
                                remark: $('input[name=remark]').val(),
                                time: $('input[name=time]').val(),
                                data: data,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('出库', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('出库', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'log') {
                window.location.href = 'WarehouseLog.html?projectId=' + projectId + '&materialId=' + data.data.materialId+ '&specId=' + data.data.specId;
            }
        });
        $('body').on('click', '#search', function () {
            var search = $('#searchInput').val();
            var materialType = $('#materialType').val();
            table.reload('lists', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    projectId: projectId,
                    type: materialType,
                    search: search,
                    token: getToken()
                }
            })
        });

        form.on('select(supplierId)', function (data) {
            if (data.value === '0') {
                $('input[name=name]').parent().show();
                $('input[name=phone]').parent().show();
                $('input[name=address]').parent().show();
            } else {
                $('input[name=name]').parent().hide();
                $('input[name=phone]').parent().hide();
                $('input[name=address]').parent().hide();
            }
        });

        form.on('select(materialId)', function (data) {
            var option = specSelect(data.value);
            $(data.elem).parent().parent().find('select[name=specId]').empty().html(option);
            form.render();
        });

        $('body').on('click', 'button.caigou.btn-plus', function () {
            var html = `<div class='layui-inline' style="margin-top:5px;">
                          <label class='layui-form-label'></label>
                          <div class='layui-input-block' style="width:25%">
                              <select name="materialId" lay-filter="materialId" lay-search>${materialSelect()}</select>
                          </div>
                          <div class='layui-input-block' style="width:15%">
                              <select name="specId" lay-filter="specId" lay-search></select>
                          </div>
                          <div class='layui-input-block' style="width:8%">
                              <input type="number" min="0" step="0.0001" class="layui-input" name="amount" placeholder="输入数量">
                          </div>
                          <div class='layui-input-block' style="width:8%">
                              <input type="text" class="layui-input" name="unitId" value=""  data-id="" disabled placeholder="计量单位">
                          </div>
                          <div class='layui-input-block' style="width:9%">
                              <input type="number" min="0" step="0.01" class="layui-input" name="price" placeholder="输入单价">
                          </div>
                          <div class='layui-input-block' style="width:15%">
                              <input type="text" class="layui-input" name="totalPrice" value="">
                          </div>
                          <div class='layui-input-block' style="width:2%">
                              <button type="button" class="layui-btn caigou btn btn-plus" style="width:26px">+</button>
                          </div>
                      </div>`
            $('.detail').append(html);
            $('.btn:not(:last)').removeClass('btn-plus').addClass('btn-minus').empty().html('-');
            form.render()
        })

        $('body').on('click', 'button.btn-minus', function () {
            $(this).parent().parent().remove();
            if ($(this).hasClass('chuku')) {
                label = '出库明细';
            } else {
                label = '入库明细';
            }
            var inline = $('.detail>div.layui-inline');
            $(inline[0]).css('margin-top', '0');
            $(inline[0]).find('label').empty().html(label);
        });

        $('body').on('input propertychange', 'input[name=amount],input[name=price]', function () {
            var detail = $(this).parent().parent()
            var amount = isNaN(Number($(detail).find('input[name=amount]').val())) ? 0 : Number($(detail).find('input[name=amount]').val());
            var price = isNaN(Number($(detail).find('input[name=price]').val())) ? 0 : Number($(detail).find('input[name=price]').val());
            $(detail).find('input[name=totalPrice]').val(Math.round(price * 100) * amount / 100);
            var div = $('.detail>div');
            var tPrice = 0;
            for (var i = 0; i < div.length; i++) {
                totalPrice = $(div[i]).find('input[name=totalPrice]').val();
                if(totalPrice === ''){
                    tmp = totalPrice
                }else{
                    a = Number($(div[i]).find('input[name=amount]').val());
                    p = Number($(div[i]).find('input[name=price]').val());
                    tmp = a * p;
                }
                tPrice += tmp;
            };
            $('input[name=tPrice]').val(tPrice)
        });

        $('body').on('input propertychange', 'input[name=totalPrice]', function () {
            let detail = $(this).parent().parent();
            let amount = isNaN(Number($(detail).find('input[name=amount]').val())) ? 0 : Number($(detail).find('input[name=amount]').val());
            let totalPrice = $(this).val();
            let price = 0
            if (amount >0){
                price = Math.floor(totalPrice*100/amount) /100
            }
            $(detail).find('input[name=price]').val(price);
            var div = $('.detail>div');
            var tPrice = 0;
            for (var i = 0; i < div.length; i++) {
                a = Number($(div[i]).find('input[name=totalPrice]').val());
                tPrice += a;
            }
            $('input[name=tPrice]').val(tPrice)
        });

        form.on('select(id)', function (data) {
            let materialId = data.value;
            let option = '<option value="" data-unit>-</option>';
            $.ajax({
                type: "post",
                url: url + '/warehouse/warehouse/specLists',
                async: false,
                data: {
                    projectId: Number(projectId),
                    materialId: materialId,
                    token: getParam('token')
                },
                success: function (r) {
                    for (let i = 0; i < r.data.length; i++) {
                        option += `<option value="${r.data[i].specId}" data-id="${r.data[i].id}" data-unitname="${r.data[i].unitName}" data-unitid="${r.data[i].unitId}" data-price="${r.data[i].salePrice==0?r.data[i].purchasePrice:r.data[i].salePrice}">${r.data[i].spec == null ? '(无规格)' : r.data[i].spec}</option>`
                    }
                }
            });

            $(data.elem).parent().parent().find('select[name=specId]').html(option);
            form.render();
        });

        form.on('select(materialId)', function (data) {
            var option = specSelect(data.value);
            $(data.elem).parent().parent().find('select[name=specId]').empty().html(option);
            form.render();
        });

        form.on('select(specId)', function (data) {
            let unitName = $(data.elem).find(`option[value="${data.value}"]`).data('unitname');
            let unitId = $(data.elem).find(`option[value="${data.value}"]`).data('unitid')
            let price = $(data.elem).find(`option[value="${data.value}"]`).data('price');
            $(data.elem).parent().parent().find('input[name=unitId]').val(unitName);
            $(data.elem).parent().parent().find('input[name=unitId]').data('id',unitId);
            $(data.elem).parent().parent().find('input[name=price]').val(Number(price));
        });

        $('body').on('click', 'button.chuku.btn-plus', function () {
            var html = `<div class='layui-inline' style="margin-top:5px;">
                           <label class='layui-form-label'></label>
                           <div class='layui-input-block' style="width:180px">
                               <select name="id" lay-filter="id" lay-search>${warehouseSelect()}</select>
                           </div>
                           <div class='layui-input-block' style="width:90px">
                                <select name="specId" lay-filter="specId" lay-search><<option value="">-</option></select>
                           </div>
                           <div class='layui-input-block' style="width:80px">
                               <input type="number" min="0" step="0.0001" class="layui-input" name="amount" placeholder="输入数量">
                           </div>
                           <div class='layui-input-block' style="width:70px">
                               <input type="text" class="layui-input" name="unitId" value="" disabled placeholder="计量单位">
                           </div>
                           <div class='layui-input-block' style="width:70px">
                               <input type="number" min="0" step="0.01" class="layui-input" name="price" placeholder="材料单价" disabled>
                           </div>
                           <div class='layui-input-block' style="width:90px">
                               <input type="text" class="layui-input" name="totalPrice" value="" placeholder="材料总价" disabled>
                           </div>
                           <div class='layui-input-block' style="width:30px">
                               <button type="button" class="layui-btn chuku btn btn-plus" style="width:26px">+</button>
                           </div>
                       </div>`
            $('.detail').append(html);
            $('.btn:not(:last)').removeClass('btn-plus').addClass('btn-minus').empty().html('-');
            form.render()
        })

        $('body').on('click', 'button.diaobo.btn-plus', function () {
            var html = `<div class='layui-inline' style="margin-top:5px;">
                          <label class='layui-form-label'></label>
                          <div class='layui-input-block' style="width:25%">
                              <select name="id" lay-filter="id" lay-search>${warehouseSelect(projectId)}</select>
                          </div>
                          <div class='layui-input-block' style="width:15%">
                              <select name="specId" lay-filter="specId" lay-search></select>
                          </div>
                          <div class='layui-input-block' style="width:8%">
                              <input type="number" min="0" step="0.0001" class="layui-input" name="amount" placeholder="输入数量">
                          </div>
                          <div class='layui-input-block' style="width:8%">
                              <input type="text" class="layui-input" name="unitId" value=""  data-id="" disabled placeholder="计量单位">
                          </div>
                          <div class='layui-input-block' style="width:2%">
                              <button type="button" class="layui-btn diaobo btn btn-plus" style="width:26px">+</button>
                          </div>
                      </div>`
            $('.detail').append(html);
            $('.btn:not(:last)').removeClass('btn-plus').addClass('btn-minus').empty().html('-');
            form.render()
        })
    });
</script>

</html>